package com.example.mr5.money.main.express;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mr5.money.R;
import com.example.mr5.money.constant.Constant;
import com.example.mr5.money.model.Record;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Calendar;

public class QueryResultActivity extends AppCompatActivity {

    private WebView resultWebView;
    private String expressName;
    private String expressNum;
    private ProgressBar progressBar;
    private AlertDialog dialog;
    private Window window;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_query_result);
        Intent intent = getIntent();
        if (intent != null){
            expressName = intent.getStringExtra("name");
            expressNum = intent.getStringExtra("num");
        }
        resultWebView = (WebView) findViewById(R.id.result_web_view);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);

        WebSettings settings = resultWebView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDefaultTextEncodingName("UTF-8");
        resultWebView.loadUrl("http://m.kuaidi100.com/index_all.html?type=" + expressName + "&postid=" + expressNum);
        resultWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                // TODO Auto-generated method stub
                if (newProgress == 100) {
                    // 网页加载完成
                    progressBar.setVisibility(View.INVISIBLE);
                } else {
                    // 加载中
                    progressBar.setVisibility(View.VISIBLE);
                }

            }
        });
    }

    private void showDialog() {
        dialog = new AlertDialog.Builder(this).create();
        dialog.show();
        window = dialog.getWindow();
        window.setContentView(R.layout.dialog_delete_item);
        final TextView dialogTitle = (TextView) window.findViewById(R.id.dialog_title);
        final TextView deleteText = (TextView) window.findViewById(R.id.delete_text);
        final TextView deleteContent = (TextView) window.findViewById(R.id.delete_content);
        dialogTitle.setText("快递查询");
        deleteContent.setText("");
        deleteText.setText("是否保存该查询记录？");

        final Button confirm = (Button) window.findViewById(R.id.confirm);
        final Button cancel = (Button) window.findViewById(R.id.cancel);

        confirm.setText("保存");
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("save",true);
                setResult(101,intent);
                finish();
                dialog.dismiss();
                dialog = null;
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                dialog = null;
            }
        });
    }


    @Override
    public void onBackPressed() {
        showDialog();
    }
}
